﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;

namespace MxWeiXinPF.Web.admin.crm
{
    public partial class tuwen_shucai_categorylist : Web.UI.ManagePage
    {
        protected int channel_id=16;
        protected string channel_name = string.Empty; //频道名称

        protected int totalCount;
        protected int page;
        protected int pageSize;
        protected string keywords = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.channel_name = new BLL.channel().GetChannelName(this.channel_id); //取得频道名称
            this.keywords = MXRequest.GetQueryString("keywords");
            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("qunfa_sucai", MXEnums.ActionEnum.View.ToString()); //检查权限
                RptBind(CombSqlTxt(this.keywords), "sort_id asc,id desc");
            }
        }

        //数据绑定
        private void RptBind(string _strWhere, string _orderby)
        {
            Model.wx_userweixin weixin = GetWeiXinCode();

            this.page = MXRequest.GetQueryInt("page", 1);
 
            this.txtKeywords.Text = this.keywords;

            BLL.article_category bll = new BLL.article_category();
            DataSet ds = bll.GetCategoryList(weixin.id, this.channel_id, this.pageSize, this.page, _strWhere, _orderby, out totalCount);
             
            this.rptList.DataSource = ds;
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("tuwen_shucai_categorylist.aspx", "keywords={0}&page={1}", this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);

        }

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append("   title like '%" + _keywords + "%'");
            }

            return strTemp.ToString();
        }
        #endregion


        #region 返回图文分类每页数量=========================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("tuwen_shucai_categorylist_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion


        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("tuwen_shucai_categorylist.aspx", "keywords={0}",  txtKeywords.Text));
        }


        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("tuwen_shucai_categorylist_page_size", _pagesize.ToString(), 43200);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("tuwen_shucai_categorylist.aspx", "keywords={0}",
               this.channel_id.ToString(), this.keywords));
        }



        //保存排序
        protected void btnSave_Click(object sender, EventArgs e)
        {
           

            ChkAdminLevel("qunfa_sucai", MXEnums.ActionEnum.Edit.ToString()); //检查权限
            BLL.article_category bll = new BLL.article_category();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), "保存" + this.channel_name + "分类排序"); //记录日志
            JscriptMsg("保存排序成功！", Utils.CombUrlTxt("tuwen_shucai_categorylist.aspx", "channel_id={0}", this.channel_id.ToString()));
        }

        //删除类别
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            Model.wx_userweixin weixin = GetWeiXinCode();
            ChkAdminLevel("qunfa_sucai", MXEnums.ActionEnum.Delete.ToString()); //检查权限
            BLL.article_category bll = new BLL.article_category();
            string ret = "";
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                
                if (cb.Checked)
                {
                   ret += bll.DeleteCatetoryAndArticle(weixin.id, id);
                }
            }
            AddAdminLog(MXEnums.ActionEnum.Delete.ToString(), "删除" + this.channel_name + "图文素材分类数据"); //记录日志
            if (ret == "")
            {
                JscriptMsg("删除数据成功！", "tuwen_shucai_categorylist.aspx");
            }
            else
            {
                JscriptMsg(ret, "tuwen_shucai_categorylist.aspx"); 
            }
        }
    }
}